package com.sys.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.common.core.domain.entity.SysUser;
import com.sys.system.domain.SysNotice;
import com.sys.system.domain.SysNoticeUser;
import com.sys.system.mapper.SysNoticeMapper;
import com.sys.system.service.INoticeUserService;
import com.sys.system.service.ISysNoticeService;
import com.sys.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 公告 服务层实现
 *
 * @author LGY
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private INoticeUserService noticeUserService;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(String noticeId) {
        SysNotice sysNotice = this.getById(noticeId);
        if (Objects.equals(sysNotice.getNoticeUserType(), 1)) {
            List<SysNoticeUser> sysNoticeUserList = noticeUserService.selectByNoticeId(noticeId);
            List<String> userIds = sysNoticeUserList.stream().map(SysNoticeUser::getUserId).toList();
            sysNotice.setNoticeUserIds(userIds);
        }
        return sysNotice;
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertNotice(SysNotice notice) {
        this.save(notice);
        // 查询
        List<SysNoticeUser> sysNoticeUserList = this.getNoticeUserList(notice);
        noticeUserService.saveBatch(sysNoticeUserList);
        noticeUserService.sendAllUserMessage();
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public void updateNotice(SysNotice notice) {
        this.updateById(notice);
        noticeUserService.deleteByNoticeIds(Collections.singletonList(notice.getId()));
        List<SysNoticeUser> sysNoticeUserList = this.getNoticeUserList(notice);
        noticeUserService.saveBatch(sysNoticeUserList);
        noticeUserService.sendAllUserMessage();
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteNoticeByIds(String[] noticeIds) {
        List<String> ids = Arrays.asList(noticeIds);
        this.removeBatchByIds(ids);
        noticeUserService.deleteByNoticeIds(ids);
        noticeUserService.sendAllUserMessage();
    }

    private List<SysNoticeUser> getNoticeUserList(SysNotice notice) {
        List<SysUser> targetUsers;
        if (Objects.equals(notice.getNoticeUserType(), 0)) {
            targetUsers = sysUserService.list().stream().filter(item -> Objects.equals(item.getDelFlag(), "0")).toList();
        } else {
            targetUsers = sysUserService.listByIds(notice.getNoticeUserIds());
        }
        return targetUsers.stream().map(user -> {
            SysNoticeUser sysNoticeUser = new SysNoticeUser();
            sysNoticeUser.setNoticeId(notice.getId())
                    .setUserId(user.getId())
                    .setStatus(0)
                    .setHasLike(0);
            return sysNoticeUser;
        }).toList();
    }

}
